@extends('admin.layout.layout')
@section('title','用户管理')
@section('con')
<div class="row">
		<div class="col-lg-12">
            <center><h1 class="page-header">用户修改</h1></center>
        </div>
	<div style='height:80px'>
		@if (count($errors)>0)
			<div class="alert alert-danger">
				<ul>
					@foreach ($errors->all() as $error)
						<li>{{ $error }}</li>
					@endforeach
				</ul>
			</div>
		@endif
	</div>
	<div class="ibox-content col-lg-8 col-lg-offset-3">
	    <form class="form-horizontal" action="{{url('/admin/user/update')}}" method='post' enctype="multipart/form-data">
	        <div class="form-group">
	            <label class="col-sm-3 control-label">用户名：</label>
	            <div class="col-sm-6">
	                <input type="text" class="form-control" name='username'  value="{{$users->username}}"> 
	            	<span></span>
	            </div>
	        </div>
	        <div class="form-group">
	            <label class="col-sm-3 control-label">密码：</label>
	            <div class="col-sm-6">
	                <input type="password" class="form-control"  name='password' value="">
	            	<span></span>
	            </div>
	        </div>
	        <div class="form-group">
	            <label class="col-sm-3 control-label">邮箱：</label>
	            <div class="col-sm-6">
	                <input type="text" class="form-control"  name='email' value="{{$users->email}}">
	            	<span></span>
	            </div>
	        </div>
	        <div class="form-group">
	            <label class="col-sm-3 control-label">手机：</label>
	            <div class="col-sm-6">
	                <input type="text" class="form-control"  name='phone' value="{{$users->phone}}">
	            	<span></span>
	            </div>
	        </div>
	        <div class="form-group">
	            <label class="col-sm-3 control-label">头像：</label>
	            <div class="col-sm-2" style='position:relative'>
	                <input type="file" placeholder="密码" name='pic' style='position:absolute;left:16px;top:5px;'>
	            	<img src="{{$users->pic}}" style='width:100px;position:absolute;left:290px;top:5px;' >
	            </div>
	        </div>
	        
	        <input type="hidden" name="uid" value="{{$users->uid}}">
	        {{csrf_field()}}
	        <div class="form-group">
	            <div class="col-sm-offset-3 col-sm-8">
	                <button type="submit" class="btn btn-sm btn-info">提交</button>
	                <button type="reset" class="btn btn-sm btn-info">重置</button>
	            </div>          
	        </div>
	    </form>
	</div>
</div>
@endsection
@section('js')
<script type="text/javascript">
//表单验证
//声明全局变量
var NAME = false;
var CPASS = false;
var EMAIL = false;
var PHONE = false;
//绑定表单事件
$('form').submit(function(){
    //触发所有的丧失焦点事件
    $('input').trigger('blur');
    //判断是否全部成功
    if(PHONE && CPASS && EMAIL && PHONE){
        return true;
    }
    return false;
});	
//给所有的字段绑定获取焦点事件
$('input').focus(function(){
    //获取属性 提示信息
    var readmin = $(this).attr('readmin');
     
})
//检查用户名
$('input[name=username]').blur(function(){
    //检查用户名是否为空
    if($(this).val() == ''){ 
        $(this).next().html('*用户名不能为空').css('color','red');
        $(this).css('border','1px solid red');
        return false;
    }
    var uname=$(this).val();
    var uid=$('input[name=uid]').val();
    var inp=$(this);
    $.get('/admin/user/check',{username:uname,uid:uid},function(data){
        // console.log(data);
        if(data==0){
            inp.next().html('√').css('color','green');
            inp.css('border','1px solid green');
            NAME = true;
        }else if(data==1){
            inp.next().html('*未修改').css('color','skyblue');
            inp.css('border','1px solid skyblue');
            NAME = true;
        }else{
        	inp.next().html('*该用户名已被其他用户注册').css('color','red');
            inp.css('border','1px solid red');
        	return false;
        }
    });
});

//检查密码
$('input[name=password]').blur(function(){
    //判断密码是否为空
    if($(this).val() == ''){ 
        $(this).next().html('*未修改密码').css('color','skyblue');
        $(this).css('border','1px solid skyblue');
        CPASS = true;
        return false;
    } 
    //判断输入密码是否符合要求
    var pass=$(this).val();
    var reg = /\w{6,18}/;       

    if(reg.test(pass)){
        $(this).next().html('√').css('color','green');
        $(this).css('border','1px solid green');
        CPASS = true;
    }else{
        $(this).next().html('密码格式不正确').css('color','red');
        $(this).css('border','1px solid red');
        CPASS = false;
    }
});

//检查手机号
$('input[name=phone]').blur(function(){
    //检查手机号是否为空
    if($(this).val() == ''){ 
        $(this).next().html('手机号不能为空').css('color','red');      
        return false;
    }
    var phone=$(this).val();
    var uid=$('input[name=uid]').val();
    var inp=$(this);
    //判断输入的手机号是否符合要求
    var reg = /^1[3-8]\d{9}$/;
    if(!reg.test(phone)){
        $(this).next().html('*手机号格式不正确').css('color','red'); 
        return false;
    }
    //发送ajax判断手机号是否已注册
    $.get('/admin/user/phone',{phone:phone,uid:uid},function(data){
        if(data==0){
            inp.next().html('√').css('color','green');
            inp.css('border','1px solid green');
            PHONE = true;
        }else if(data==1){
            inp.next().html('*未修改').css('color','skyblue');
            inp.css('border','1px solid skyblue');
            PHONE = true;
        }else{
        	inp.next().html('*该手机号已被其他用户注册').css('color','red');
            inp.css('border','1px solid red');
        	return false;
        }
    });
});

//检查邮箱
$('input[name=email]').blur(function(){
    //检查用户名是否为空
    if($(this).val() == ''){ 
        $(this).next().html('邮箱不能为空').css('color','red');      
        return false;
    }
    var email=$(this).val();
    var uid=$('input[name=uid]').val();
    var inp=$(this);
    //判断输入的邮箱是否符合要求
    var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if(!reg.test(email)){
        $(this).next().html('邮箱格式不正确').css('color','red');
        return false;
    }
    //发送ajax判断邮箱号是否已注册
    $.get('/admin/user/email',{email:email,uid:uid},function(data){
        if(data==0){
            inp.next().html('√').css('color','green');
            inp.css('border','1px solid green');
            EMAIL = true;
        }else if(data==1){
            inp.next().html('*未修改').css('color','skyblue');
            inp.css('border','1px solid skyblue');
            EMAIL = true;
        }else{
        	inp.next().html('*该邮箱已被其他用户注册').css('color','red');
            inp.css('border','1px solid red');
        	return false;
        }
    });
});






</script>
@endsection